<!DOCTYPE html>
<html lang=en>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="有时候异常处理的代码散布在项目代码的各个地方，许多相同的异常处理，有可能在非常多的类中都有出现，这不仅造成了代码冗余，不断编写重复代码，更是增加了代码的耦合性，在后期异常不断增多的情况下，维护起来也是非常的麻烦，于是可以利用AOP的思想统一异常处理。拿Shiro的Exception来说，在controller中，登陆接口为&#x2F;login12345678910111213141516171819202">
<meta name="keywords" content="Java,SpringBoot">
<meta property="og:type" content="article">
<meta property="og:title" content="SpringBoot异常处理">
<meta property="og:url" content="http:&#x2F;&#x2F;yoursite.com&#x2F;2019&#x2F;11&#x2F;06&#x2F;SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86&#x2F;index.html">
<meta property="og:site_name" content="ECIN&#39;S BLOG">
<meta property="og:description" content="有时候异常处理的代码散布在项目代码的各个地方，许多相同的异常处理，有可能在非常多的类中都有出现，这不仅造成了代码冗余，不断编写重复代码，更是增加了代码的耦合性，在后期异常不断增多的情况下，维护起来也是非常的麻烦，于是可以利用AOP的思想统一异常处理。拿Shiro的Exception来说，在controller中，登陆接口为&#x2F;login12345678910111213141516171819202">
<meta property="og:locale" content="en">
<meta property="og:updated_time" content="2019-11-06T15:17:42.000Z">
<meta name="twitter:card" content="summary">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
          
        
    
    <!-- title -->
    <title>SpringBoot异常处理</title>
    <!-- styles -->
    <link rel="stylesheet" href="/css/style.css">
    <!-- persian styles -->
    
      <link rel="stylesheet" href="/css/rtl.css">
    
    <!-- rss -->
    
    
</head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2019/11/08/java%E9%98%BF%E9%87%8C%E5%B7%B4%E5%B7%B4%E5%BC%80%E5%8F%91%E6%89%8B%E5%86%8C/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2019/11/04/%E4%BD%BF%E7%94%A8lombok%E6%B3%A8%E8%A7%A3%E7%AE%80%E5%8C%96%E4%BB%A3%E7%A0%81%E5%BC%80%E5%8F%91/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">Share post</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/" target="_blank" rel="noopener"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&text=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&title=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&is_video=false&description=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=SpringBoot异常处理&body=Check out this article: http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/" target="_blank" rel="noopener"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&title=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&title=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&title=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&title=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&name=SpringBoot异常处理&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&t=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-6"><a class="toc-link" href="#有时候异常处理的代码散布在项目代码的各个地方，许多相同的异常处理，有可能在非常多的类中都有出现，这不仅造成了代码冗余，不断编写重复代码，更是增加了代码的耦合性，在后期异常不断增多的情况下，维护起来也是非常的麻烦，于是可以利用AOP的思想统一异常处理。"><span class="toc-number">1.</span> <span class="toc-text">有时候异常处理的代码散布在项目代码的各个地方，许多相同的异常处理，有可能在非常多的类中都有出现，这不仅造成了代码冗余，不断编写重复代码，更是增加了代码的耦合性，在后期异常不断增多的情况下，维护起来也是非常的麻烦，于是可以利用AOP的思想统一异常处理。</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#拿Shiro的Exception来说，在controller中，登陆接口为-login"><span class="toc-number"></span> <span class="toc-text">拿Shiro的Exception来说，在controller中，登陆接口为/login</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#新建一个全局异常处理类ExceptionHandle"><span class="toc-number"></span> <span class="toc-text">新建一个全局异常处理类ExceptionHandle</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#RestControllerAdvice相当于-ControllerAdvice-ResponseBody，表示这是一个全局异常处理的类。"><span class="toc-number">1.</span> <span class="toc-text">@RestControllerAdvice相当于@ControllerAdvice+@ResponseBody，表示这是一个全局异常处理的类。</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#然后如果捕获到相应异常就会返回ExceptionHandle中的异常处理相应的方法。这是一个简单的例子，虽然这样可以简化一些代码，但是这只能处理简单的异常处理，而且仅限于Controller层。"><span class="toc-number"></span> <span class="toc-text">然后如果捕获到相应异常就会返回ExceptionHandle中的异常处理相应的方法。这是一个简单的例子，虽然这样可以简化一些代码，但是这只能处理简单的异常处理，而且仅限于Controller层。</span></a>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        SpringBoot异常处理
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">ECIN'S BLOG</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2019-11-06T12:18:07.000Z" itemprop="datePublished">2019-11-06</time>
        
      
    </div>


      

      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link" href="/tags/Java/" rel="tag">Java</a>, <a class="tag-link" href="/tags/SpringBoot/" rel="tag">SpringBoot</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h6 id="有时候异常处理的代码散布在项目代码的各个地方，许多相同的异常处理，有可能在非常多的类中都有出现，这不仅造成了代码冗余，不断编写重复代码，更是增加了代码的耦合性，在后期异常不断增多的情况下，维护起来也是非常的麻烦，于是可以利用AOP的思想统一异常处理。"><a href="#有时候异常处理的代码散布在项目代码的各个地方，许多相同的异常处理，有可能在非常多的类中都有出现，这不仅造成了代码冗余，不断编写重复代码，更是增加了代码的耦合性，在后期异常不断增多的情况下，维护起来也是非常的麻烦，于是可以利用AOP的思想统一异常处理。" class="headerlink" title="有时候异常处理的代码散布在项目代码的各个地方，许多相同的异常处理，有可能在非常多的类中都有出现，这不仅造成了代码冗余，不断编写重复代码，更是增加了代码的耦合性，在后期异常不断增多的情况下，维护起来也是非常的麻烦，于是可以利用AOP的思想统一异常处理。"></a>有时候异常处理的代码散布在项目代码的各个地方，许多相同的异常处理，有可能在非常多的类中都有出现，这不仅造成了代码冗余，不断编写重复代码，更是增加了代码的耦合性，在后期异常不断增多的情况下，维护起来也是非常的麻烦，于是可以利用AOP的思想统一异常处理。</h6><h5 id="拿Shiro的Exception来说，在controller中，登陆接口为-login"><a href="#拿Shiro的Exception来说，在controller中，登陆接口为-login" class="headerlink" title="拿Shiro的Exception来说，在controller中，登陆接口为/login"></a>拿Shiro的Exception来说，在controller中，登陆接口为/login</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/login"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">Login</span><span class="params">(@RequestParam(<span class="string">"username"</span>)</span> String username,</span></span><br><span class="line"><span class="function">                    @<span class="title">RequestParam</span><span class="params">(<span class="string">"password"</span>)</span>String password,</span></span><br><span class="line"><span class="function">                    @<span class="title">RequestParam</span><span class="params">(<span class="string">"remember"</span>)</span><span class="keyword">boolean</span> remember)</span>&#123;</span><br><span class="line">    <span class="comment">/**创建一个subject*/</span></span><br><span class="line">    Subject subject = SecurityUtils.getSubject();</span><br><span class="line">    <span class="comment">/**封装用户数据*/</span></span><br><span class="line">    UsernamePasswordToken token = <span class="keyword">new</span> UsernamePasswordToken(username,password);</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">        <span class="comment">/**执行登录,然后这里会跳转到Realm的认证逻辑中*/</span></span><br><span class="line">        subject.login(token);</span><br><span class="line">        token.setRememberMe(remember);</span><br><span class="line">        Session session = SecurityUtils.getSubject().getSession();</span><br><span class="line">        <span class="keyword">return</span> session.getId().toString();</span><br><span class="line">    &#125; <span class="keyword">catch</span> (UnknownAccountException uae) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"未知账户"</span>;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (IncorrectCredentialsException ice) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"密码不正确"</span>;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (LockedAccountException lae) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"账户已锁定"</span>;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (ExcessiveAttemptsException eae) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"用户名或密码错误次数过多"</span>;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (AuthenticationException ae) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"用户名或密码不正确！"</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>可以发现有很多的try…catch语句</p>
<h5 id="新建一个全局异常处理类ExceptionHandle"><a href="#新建一个全局异常处理类ExceptionHandle" class="headerlink" title="新建一个全局异常处理类ExceptionHandle"></a>新建一个全局异常处理类ExceptionHandle</h5><h6 id="RestControllerAdvice相当于-ControllerAdvice-ResponseBody，表示这是一个全局异常处理的类。"><a href="#RestControllerAdvice相当于-ControllerAdvice-ResponseBody，表示这是一个全局异常处理的类。" class="headerlink" title="@RestControllerAdvice相当于@ControllerAdvice+@ResponseBody，表示这是一个全局异常处理的类。"></a>@RestControllerAdvice相当于@ControllerAdvice+@ResponseBody，表示这是一个全局异常处理的类。</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.apache.shiro.authc.*;</span><br><span class="line"><span class="keyword">import</span> org.apache.shiro.authz.AuthorizationException;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.ExceptionHandler;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RestControllerAdvice;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.HashMap;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">/**定义全局异常处理类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> NICE*/</span></span><br><span class="line"><span class="meta">@RestControllerAdvice</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ExceptionHandle</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ExceptionHandler</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">errorHandler</span><span class="params">(AuthorizationException e)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">"Sorry,you don't have permission."</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ExceptionHandler</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> HashMap&lt;Integer, String&gt; <span class="title">unknownAccountException</span><span class="params">(UnknownAccountException e)</span></span>&#123;</span><br><span class="line">        HashMap&lt;Integer, String&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;(<span class="number">1</span>);</span><br><span class="line">        map.put(<span class="number">500</span>,<span class="string">"Unknown Account."</span>);</span><br><span class="line">        <span class="keyword">return</span> map;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ExceptionHandler</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> HashMap&lt;Integer, String&gt; <span class="title">incorrectCredentialsException</span><span class="params">(IncorrectCredentialsException e)</span></span>&#123;</span><br><span class="line">        HashMap&lt;Integer, String&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;(<span class="number">1</span>);</span><br><span class="line">        map.put(<span class="number">500</span>,<span class="string">"Password Wrong."</span>);</span><br><span class="line">        <span class="keyword">return</span> map;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ExceptionHandler</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> HashMap&lt;Integer, String&gt; <span class="title">lockedAccountException</span><span class="params">(LockedAccountException e)</span></span>&#123;</span><br><span class="line">        HashMap&lt;Integer, String&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;(<span class="number">1</span>);</span><br><span class="line">        map.put(<span class="number">500</span>,<span class="string">"Account Locked."</span>);</span><br><span class="line">        <span class="keyword">return</span> map;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ExceptionHandler</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> HashMap&lt;Integer, String&gt; <span class="title">excessiveAttemptsException</span><span class="params">(ExcessiveAttemptsException e)</span></span>&#123;</span><br><span class="line">        HashMap&lt;Integer, String&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;(<span class="number">1</span>);</span><br><span class="line">        map.put(<span class="number">500</span>,<span class="string">"Too Much Input Wrong."</span>);</span><br><span class="line">        <span class="keyword">return</span> map;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ExceptionHandler</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> HashMap&lt;Integer, String&gt; <span class="title">authenticationException</span> <span class="params">(AuthenticationException e)</span></span>&#123;</span><br><span class="line">        HashMap&lt;Integer, String&gt; map = <span class="keyword">new</span> HashMap&lt;&gt;(<span class="number">1</span>);</span><br><span class="line">        map.put(<span class="number">500</span>,<span class="string">"Username Or Password Wrong."</span>);</span><br><span class="line">        <span class="keyword">return</span> map;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="然后如果捕获到相应异常就会返回ExceptionHandle中的异常处理相应的方法。这是一个简单的例子，虽然这样可以简化一些代码，但是这只能处理简单的异常处理，而且仅限于Controller层。"><a href="#然后如果捕获到相应异常就会返回ExceptionHandle中的异常处理相应的方法。这是一个简单的例子，虽然这样可以简化一些代码，但是这只能处理简单的异常处理，而且仅限于Controller层。" class="headerlink" title="然后如果捕获到相应异常就会返回ExceptionHandle中的异常处理相应的方法。这是一个简单的例子，虽然这样可以简化一些代码，但是这只能处理简单的异常处理，而且仅限于Controller层。"></a>然后如果捕获到相应异常就会返回ExceptionHandle中的异常处理相应的方法。这是一个简单的例子，虽然这样可以简化一些代码，但是这只能处理简单的异常处理，而且仅限于Controller层。</h5>
  </div>
</article>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-6"><a class="toc-link" href="#有时候异常处理的代码散布在项目代码的各个地方，许多相同的异常处理，有可能在非常多的类中都有出现，这不仅造成了代码冗余，不断编写重复代码，更是增加了代码的耦合性，在后期异常不断增多的情况下，维护起来也是非常的麻烦，于是可以利用AOP的思想统一异常处理。"><span class="toc-number">1.</span> <span class="toc-text">有时候异常处理的代码散布在项目代码的各个地方，许多相同的异常处理，有可能在非常多的类中都有出现，这不仅造成了代码冗余，不断编写重复代码，更是增加了代码的耦合性，在后期异常不断增多的情况下，维护起来也是非常的麻烦，于是可以利用AOP的思想统一异常处理。</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#拿Shiro的Exception来说，在controller中，登陆接口为-login"><span class="toc-number"></span> <span class="toc-text">拿Shiro的Exception来说，在controller中，登陆接口为/login</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#新建一个全局异常处理类ExceptionHandle"><span class="toc-number"></span> <span class="toc-text">新建一个全局异常处理类ExceptionHandle</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#RestControllerAdvice相当于-ControllerAdvice-ResponseBody，表示这是一个全局异常处理的类。"><span class="toc-number">1.</span> <span class="toc-text">@RestControllerAdvice相当于@ControllerAdvice+@ResponseBody，表示这是一个全局异常处理的类。</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#然后如果捕获到相应异常就会返回ExceptionHandle中的异常处理相应的方法。这是一个简单的例子，虽然这样可以简化一些代码，但是这只能处理简单的异常处理，而且仅限于Controller层。"><span class="toc-number"></span> <span class="toc-text">然后如果捕获到相应异常就会返回ExceptionHandle中的异常处理相应的方法。这是一个简单的例子，虽然这样可以简化一些代码，但是这只能处理简单的异常处理，而且仅限于Controller层。</span></a>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/" target="_blank" rel="noopener"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&text=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&title=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&is_video=false&description=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=SpringBoot异常处理&body=Check out this article: http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/" target="_blank" rel="noopener"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&title=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&title=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&title=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&title=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&name=SpringBoot异常处理&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2019/11/06/SpringBoot%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/&t=SpringBoot异常处理" target="_blank" rel="noopener"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> Menu</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> TOC</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> Share</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> Top</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 ECIN520
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">

    <!-- jquery -->
<script src="/lib/jquery/jquery.min.js"></script>
<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>
<!-- clipboard -->

  <script src="/lib/clipboard/clipboard.min.js"></script>
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"Copy to clipboard!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "Copied!");
      e.clearSelection();
    })
  })
  </script>

<script src="/js/main.js"></script>
<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Disqus Comments -->


</body>
</html>
